{
  "properties" : { },
  "id" : "ead52cf3abcd4ed0b5c9b5fe1e7d4fe7",
  "script" : null,
  "groupId" : "5068d5bd296f4017badae51e5b6c34c7",
  "name" : "查询对话状态",
  "createTime" : 1741019539835,
  "updateTime" : 1741367987705,
  "lock" : null,
  "createBy" : null,
  "updateBy" : null,
  "path" : "/chat_list",
  "cron" : "*/10 * * * * ?",
  "enabled" : false,
  "description" : null
}
================================
import response;
import log
import http

// 这里要改成对话记录的返回
log.info("开始定时任务--->")
var chatList = db.table("chat_record").where().eq("ai_status", "in_progress").ne("ai_status", "cancel").select()
if (chatList.size() > 0) {
    log.info("进来这里--->")
    for (item in chatList) {
        log.info("item" + item::string)
        let requestUrl = "https://api.coze.cn/v3/chat/message/list?" + "chat_id=" + item.chatId + "&conversation_id=" + item.conversationId
        log.info("requestUrl-->" + requestUrl)
        let appkey = "pat_lAeSswkKc7Bp88MLcqPK3faFZtStgrEKCN9I8iOvMxbcY0IOTFYR42I3oDRxNJV0"
        var result = http.connect(requestUrl).contentType('application/json').header("Authorization", "Bearer " + appkey).get().getBody()
        log.info("result-->" + result)
        if (result.code == 0) {
            if (result.data != null) {
                let dataList = result.data
                for (item2 in dataList) {
                    if (item2.type == "answer") {
                        //更新到表记录
                        var db_res = db.table("chat_record").primary("id").update({
                            id: item.id,
                            ai_status: "completed", //更新状态
                            ai_result: item2.content
                        })
                        //处理免费订阅客户
                        //如果用户是免费用户
                        var subscri = db.table("subscription").where().eq("user_id", item.userId).eq("package_id", 1) selectOne()
                        if (subscri != null) {
                            //扣减余额表
                            var credit = db.table("credit").where().eq("user_id", userId).eq("package_id", 1).selectOne()
                            if (credit != null) {
                                if (credit.remainCount > 0) {
                                    db.table("credit").primary("id").update({
                                        id: credit.id,
                                        remainCount: credit.remainCount - 1
                                    })
                                }
                            }
                            db.table("")
                        }
                        log.info("db_res-->" + db_res)
                    }
                }
            }

        }
    }

}